import os, re, sys

def findDocPref():
	population = {}
	for fn in os.listdir("."):
		cpref = re.split("-[0-9]{3}", fn)
		if len(cpref) <= 1:
			continue
		cpref = cpref[0]
		if not population.has_key(cpref):
			population[cpref] = 0
		population[cpref] += 1
	bp = 0
	ans = "?"
	for (k, v) in population.items():
		if v > bp:
			bp = v
			ans = k
	return ans			

def findBiggestNum(docpref):
	biggest = -1
	bs = "?"
	for fn in os.listdir("."):
		if fn.startswith(docpref):
			end = fn[len(docpref)+1:]
			end = end[:3]
			num = int(end)
			if num > biggest:
				biggest = num
				bs = fn
	return (bs, biggest)

def normNum(num):
	rem = 3 - len(num)
	if rem < 0:
		rem = 0
	return "0" * rem + num

def findExt(docpref):
	for fn in os.listdir("."):
		if fn.startswith(docpref):
			return os.path.splitext(fn)[1]

docpref = findDocPref()
(biggest, biggestNum) = findBiggestNum(docpref)
next = biggestNum + 1
next = str(next)
next = normNum(next)
comment = ""
if len(sys.argv) > 1:
	comment = "-" + sys.argv[1]
nextname = docpref + "-" + next + comment + findExt(docpref)
assert os.path.exists(os.path.join(".", biggest))
assert not os.path.exists(os.path.join(".", nextname))
os.system("cp " + biggest + " " + nextname)
